package com.zksb.controller;

import com.zksb.entity.Food;
import com.zksb.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/foods")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @GetMapping
    public Object find(HttpServletRequest request) {
        List<Food> foods = foodService.find();

        request.setAttribute("foodList", foods);

        return "foodList";
    }

    @GetMapping("/{id:\\d+}")
    public Object get(@PathVariable("id") Integer id) {
        return foodService.get(id);
    }
}
